package com.hbmcc.deviceinfo.ui.main


import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hbmcc.deviceinfo.databinding.MainListItemBinding

/**
 * @author 支豪亮
 * * @des 业务左侧的Adapter
 */
class MainAdapter(val callback: MainCallback) : RecyclerView.Adapter<MainAdapter.ViewHolder>() {

    val listData: MutableList<MainBean> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding =
                MainListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.viewModel = listData[position]
        holder.binding.callback = callback
        holder.binding.position = position
    }

    override fun getItemCount(): Int {
        return listData.size
    }

    fun updateListData(list: MutableList<MainBean>) {
        listData.clear()
        listData.addAll(list)
        notifyDataSetChanged()
    }

    fun updateData(mainBean: MainBean) {
        listData.add(mainBean)
        notifyDataSetChanged()
    }

    class ViewHolder(var binding: MainListItemBinding) : RecyclerView.ViewHolder(binding.root)

}